<?php
class show_style_model extends MY_model
{
    public $id;
    public $code;
    public $style;

    public static $CODE = 'STYLE_CODE';
    public static $STYLE_SLIDE = 'SLIDE';
    public static $STYLE_NEW = 'NEW';

    public function __construct()
    {
        parent::__construct();
        $this->table_name = 'show_style';
    }

    public static function get_elements()
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM show_style ORDER BY modified_date DESC");
        return self::to_domain_models($query->result());
    }


    public static function get_element_by_id($id)
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM show_style WHERE id = ?",array($id));
        return self::to_domain_model($query->row());
    }

    public static function get_element_by_code()
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM show_style WHERE code = ?",array(self::$CODE));
        return self::to_domain_model($query->row());
    }

    // -------------------------------------------------------------------------------- PRIVATE METHOD

    // Private method
    private static function to_domain_models($array)
    {
        $result = array();
        foreach($array as $item)
        {
            $result[] = show_style_model::to_domain_model($item);
        }
        return $result;
    }

    // Private method
    private static function to_domain_model($item)
    {
        $model = new show_style_model();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->code = isset($item->code) ? $item->code : '' ;
        $model->style = isset($item->style) ? $item->style : '' ;
        return $model;
    }

    protected function to_array()
    {
        $data_arr = array(
            'code' => $this->code,
            'style' => $this->style
        );

        return $data_arr;
    }
}